package ch0001;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

/*
docker run -d --rm --network bridge -p 3306:3306 --env MARIADB_ROOT_PASSWORD=root mariadb

use studb;
create table student(
name varchar(10) primary key,
age int,
tall int,
weight int,
address varchar(30),
school varchar(30),
depart varchar(30)
);

insert into student values('zhangsan',18,188,'henan','sias','computer');
insert into student values('lisi',21,170,'henan','sias','computer');
insert into student(name,age,address) values('wangwu',22,'henan xinzheng');

string sql_insert="insert into student(name,age,tall,weight,address,school,depart) values(name,age,tall,weight,address,school,depart);"
string sql_insert="insert into student values('"+name1+"','"+age1+"','"+height1+"','"weight1+"','"+address1+"','"+school+"','"+depart+"');"
 * */
public class MyFrame5_2 extends JFrame {
	JLabel label_name;
	JTextField text_name;
	JLabel label_school;
	String[] schools = { "郑州大学", "河南大学", "河南科技大学", "河南工业大学" };
	JComboBox combo1;
	JLabel label_job;
	String[] xbs = { "计科", "多媒体", "电子信息", "计算机维护" };
	JList list1;
	JMenuBar bar1;
	JMenu menufile, menufind, menuedit, menuformat;
	JMenuItem itemopen, itemsave, itemclose;
	JMenuItem itemcut, itemcopy, itempaste;
	JMenuItem itementer, itemfonts, itempara;

	JMenuItem itemfind, itemreplace;

	JButton button1;
	JLabel label_age, label_height, label_weight, label_address;
	JTextField text_age, text_height, text_weight, text_address;

	public MyFrame5_2() {
		setTitle("学生管理系统");
		setSize(400, 300);
		setResizable(false);
		button1 = new JButton("确定");
		button1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String name1 = text_name.getText().trim();
				String age1 = text_age.getText().trim();
				String height1 = text_height.getText().trim();
				String weight1 = text_weight.getText().trim();
				String address1 = text_address.getText().trim();
				String school1 = (String) combo1.getSelectedItem();
				String depart1 = (String) list1.getSelectedValue();

				System.out.println(name1);
				System.out.println(age1);
				System.out.println(height1);
				System.out.println(weight1);
				System.out.println(address1);
				System.out.println(school1);
				System.out.println(depart1);
				String driver = "com.mysql.jdbc.Driver";
//				String url = "jdbc:mysql://172.25.30.199:3306/studb?user=root;password=root";
				String url = "jdbc:mysql://172.25.30.199:3306/studb?characterEncoding=gbk";
				java.sql.Connection conn = null;
				try {
					// 创建数据库链接
					Class.forName(driver);
					conn = DriverManager.getConnection(url, "root", "root");
					if (conn != null) {
						System.out.println("connect success!");
						String sql_insert = "insert into student values('" + name1 + "','" + age1 + "','" + height1
								+ "','" + weight1 + "','" + address1 + "','" + school1 + "','" + depart1 + "');";
						System.out.println(sql_insert);

						java.sql.Statement stmt = conn.createStatement();
						int a = stmt.executeUpdate(sql_insert);
						if (a == 1)
							System.out.println("insert success!");

					}
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				try {
					conn.close(); // 关闭数据库链接
				} catch (Exception ex) {
					ex.printStackTrace();
				}

			}

		});
		Container c1 = getContentPane();
		//
		GridBagLayout gb1 = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		// c1.setLayout(new FlowLayout());
		c1.setLayout(gb1);

		label_name = new JLabel("姓名");
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(label_name, gbc);
		c1.add(label_name);

		text_name = new JTextField(10);
		gbc.gridx = 2;
		gbc.gridy = 1;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(text_name, gbc);
		c1.add(text_name);

		label_age = new JLabel("年龄");
		gbc.gridx = 3;
		gbc.gridy = 1;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(label_age, gbc);
		c1.add(label_age);

		text_age = new JTextField(10);
		gbc.gridx = 4;
		gbc.gridy = 1;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(text_age, gbc);
		c1.add(text_age);

		label_height = new JLabel("身高");
		gbc.gridx = 1;
		gbc.gridy = 2;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(label_height, gbc);
		c1.add(label_height);

		text_height = new JTextField(10);
		gbc.gridx = 2;
		gbc.gridy = 2;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(text_height, gbc);
		c1.add(text_height);

		label_weight = new JLabel("体重");
		gbc.gridx = 3;
		gbc.gridy = 2;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(label_weight, gbc);
		c1.add(label_weight);

		text_weight = new JTextField(10);
		gbc.gridx = 4;
		gbc.gridy = 2;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(text_weight, gbc);
		c1.add(text_weight);

		label_address = new JLabel("家庭住址");
		gbc.gridx = 1;
		gbc.gridy = 3;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(label_address, gbc);
		c1.add(label_address);

		text_address = new JTextField(10);
		gbc.gridx = 2;
		gbc.gridy = 3;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(text_address, gbc);
		c1.add(text_address);

		label_school = new JLabel("选择学校：");
		gbc.gridx = 1;
		gbc.gridy = 4;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(label_school, gbc);
		c1.add(label_school);

		combo1 = new JComboBox(schools);
		gbc.gridx = 2;
		gbc.gridy = 4;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(combo1, gbc);
		c1.add(combo1);

		label_job = new JLabel("专业");
		gbc.gridx = 1;
		gbc.gridy = 5;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(label_job, gbc);
		c1.add(label_job);
		list1 = new JList(xbs);
		// c1.add(list1);
		list1.setVisibleRowCount(1);
		JScrollPane jp1 = new JScrollPane(list1);
		gbc.gridx = 2;
		gbc.gridy = 5;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(jp1, gbc);
		c1.add(jp1);

		gbc.gridx = 1;
		gbc.gridy = 6;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gb1.setConstraints(button1, gbc);
		c1.add(button1);

		bar1 = new JMenuBar();
		menufile = new JMenu("文件");
		itemopen = new JMenuItem("打开");
		itemclose = new JMenuItem("关闭");
		itemsave = new JMenuItem("保存");
		menufile.add(itemopen);
		menufile.add(itemclose);
		menufile.add(itemsave);

		menuedit = new JMenu("编辑");
		itemcut = new JMenuItem("剪切");
		itemcopy = new JMenuItem("复制");
		itempaste = new JMenuItem("粘贴");
		menuedit.add(itemcut);
		menuedit.add(itemcopy);
		menuedit.add(itempaste);

		menuformat = new JMenu("格式");
		itementer = new JMenuItem("自动换行");
		itemfonts = new JMenuItem("字体");
		itempara = new JMenuItem("段落");
		menuformat.add(itementer);
		menuformat.add(itemfonts);
		menuformat.add(itempara);

		bar1.add(menufile);
		bar1.add(menuedit);
		bar1.add(menuformat);
		setJMenuBar(bar1);

		setVisible(true);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyFrame5_2 myf5_2 = new MyFrame5_2();
		myf5_2.show();
		myf5_2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}
